package com.android.lovely.ui.fragment

import android.graphics.Rect
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.android.lovely.R
import com.android.lovely.const.Gif
import com.android.lovely.databinding.FragmentHomeBinding
import com.android.lovely.databinding.FragmentHomeListBinding
import com.android.lovely.ext.init
import com.android.lovely.framework.BaseFragment
import com.android.lovely.ui.adapter.HomeListAdapter
import com.android.lovely.ui.viewmodel.HomeListViewModel
import com.android.lovely.ui.viewmodel.HomeViewModel
import com.kotlin.extensions.bus.FlowBus
import com.kotlin.extensions.bus.flowOf
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.dimen.Dimension.dp
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/**
 *
 * @author why
 * @date 2025/3/10 15:14
 */
class HomeListFragment : BaseFragment<FragmentHomeListBinding>() {
    private val tab by lazy { arguments?.getInt("tab") }
    private val viewModel by viewModels<HomeListViewModel>()

    private val adapter by lazy { HomeListAdapter() }


    private val homeViewModel by activityViewModels<HomeViewModel>()
    override fun initView() {
        viewModel.tab = arguments?.getInt("tab")
        mBinding.rv.addItemDecoration(HomeListItemDecoration(10.dp));

        mBinding.rv.adapter = adapter.buildAdapter()
        mBinding.root.init()
        mBinding.root.setOnRefreshListener {
            adapter.refresh()
        }
        mBinding.flexState.setLoadState(adapter)
        mBinding.flexState.setNoContentIcon(R.drawable.icon_no_search)
        mBinding.flexState.setNoContentText(getString(R.string.no_role))
        mBinding.flexState.setNoContentButton(getString(R.string.create_role), false) {

        }
        viewLifecycleOwner.lifecycleScope.launchWhenResumed {
            viewModel.pager.collectLatest {
                adapter.submitData(it)
            }
        }
        adapter.addOnPagesUpdatedListener {
            mBinding.root.isRefreshing = false
            if (viewModel.currentPage == 1) {
                mBinding.rv.scrollToPosition(0)
            }
        }
        viewLifecycleOwner.lifecycleScope.launch {
            FlowBus.flowOf<Gif>().collectLatest {
                adapter.notifyDataSetChanged()
            }
        }
    }

    override suspend fun initData() {
        homeViewModel.homeTabList2.collectLatest {
            mBinding.rv.scrollToPosition(0)
            mBinding.flexState.displayProgress()
            adapter.refresh()
        }
    }
}

class HomeListItemDecoration(private val space: Int) : ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
// 获取当前 item 的位置
        val position = parent.getChildAdapterPosition(view)
        // 获取列数
        val spanCount = (parent.layoutManager as? GridLayoutManager)?.spanCount ?: 1

        // 计算当前 item 所在的列
        val column = position % spanCount

        // 设置左右间距
        outRect.left = space - column * space / spanCount
        outRect.right = (column + 1) * space / spanCount

        // 设置上下间距
        if (position >= spanCount) {
            outRect.top = space
        }
    }
}